﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib;
using WFNetLib.WinAPI;

namespace 邻苯浓度测量仪
{
    public partial class Form2 : Form
    {
        public Form2(string portName)
        {
            InitializeComponent();
            serialPort1.Close();
            serialPort1.PortName = portName;
            serialPort1.ReadTimeout = 500;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            if (!WFGlobal.OpenSerialPort(ref serialPort1, "所选"))
            {
                this.Close();
                return;
            }
            ComParket ret = ComParket.ComProc(ref serialPort1, 0x0001, null);
            if (ret == null)
            {
                MessageBox.Show("无法从设备中采集到数据，请检查连接情况");
                this.Close();
                return;
            }
            else
            {
                k1 = ComParket.ComProc_Byte2Float(ret.Data, ComParketHead.HEAD_SIZE);
                b1 = ComParket.ComProc_Byte2Float(ret.Data, ComParketHead.HEAD_SIZE + 4);
                k2 = ComParket.ComProc_Byte2Float(ret.Data, ComParketHead.HEAD_SIZE + 8);
                b2 = ComParket.ComProc_Byte2Float(ret.Data, ComParketHead.HEAD_SIZE + 12);
                k3 = ComParket.ComProc_Byte2Float(ret.Data, ComParketHead.HEAD_SIZE + 16);
                b3 = ComParket.ComProc_Byte2Float(ret.Data, ComParketHead.HEAD_SIZE + 20);
                tbK1.Text = k1.ToString("f4");
                tbK2.Text = k2.ToString("f4");
                tbK3.Text = k3.ToString("f4");
                tbB1.Text = b1.ToString("f4");
                tbB2.Text = b2.ToString("f4");
                tbB3.Text = b3.ToString("f4");
            }
            serialPort1.ReadTimeout = 3000;
        }
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            serialPort1.Close();
        }
        float c1,c2,c3,c4,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,k1,k2,k3,b1,b2,b3;
        private void button1_Click(object sender, EventArgs e)
        {
            ComParket ret = ComParket.ComProc(ref serialPort1, 0x0003, null);
            if (ret == null)
            {
                MessageBox.Show("无法从设备中采集到数据，请检查连接情况");
            }
            else
            {
                v1 = ComParket.ComProc_Byte2Float(ret.Data,ComParketHead.HEAD_SIZE);
                v2 = ComParket.ComProc_Byte2Float(ret.Data,ComParketHead.HEAD_SIZE+4);
                tbV1.Text = v1.ToString("f4");
                tbV2.Text = v2.ToString("f4");
            }
            Win32API.MessageBeep(APIConstant.MB_ICONEXCLAMATION);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ComParket ret = ComParket.ComProc(ref serialPort1, 0x0005, null);
            if (ret == null)
            {
                MessageBox.Show("无法从设备中采集到数据，请检查连接情况");
            }
            else
            {
                v3 = ComParket.ComProc_Byte2Float(ret.Data,ComParketHead.HEAD_SIZE);
                v4 = ComParket.ComProc_Byte2Float(ret.Data,ComParketHead.HEAD_SIZE+4);
                tbV3.Text = v3.ToString("f4");
                tbV4.Text = v4.ToString("f4");
            }
            Win32API.MessageBeep(APIConstant.MB_ICONEXCLAMATION);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ComParket ret = ComParket.ComProc(ref serialPort1, 0x0007, null);
            if (ret == null)
            {
                MessageBox.Show("无法从设备中采集到数据，请检查连接情况");
            }
            else
            {
                v5 = ComParket.ComProc_Byte2Float(ret.Data,ComParketHead.HEAD_SIZE);
                v6 = ComParket.ComProc_Byte2Float(ret.Data,ComParketHead.HEAD_SIZE+4);
                tbV5.Text = v5.ToString("f4");
                tbV6.Text = v6.ToString("f4");
            }
            Win32API.MessageBeep(APIConstant.MB_ICONEXCLAMATION);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ComParket ret = ComParket.ComProc(ref serialPort1, 0x0005, null);
            if (ret == null)
            {
                MessageBox.Show("无法从设备中采集到数据，请检查连接情况");
            }
            else
            {
                v7 = ComParket.ComProc_Byte2Float(ret.Data,ComParketHead.HEAD_SIZE);
                v8 = ComParket.ComProc_Byte2Float(ret.Data,ComParketHead.HEAD_SIZE+4);
                tbV7.Text = v7.ToString("f4");
                tbV8.Text = v8.ToString("f4");
            }
            Win32API.MessageBeep(APIConstant.MB_ICONEXCLAMATION);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ComParket ret = ComParket.ComProc(ref serialPort1, 0x0007, null);
            if (ret == null)
            {
                MessageBox.Show("无法从设备中采集到数据，请检查连接情况");
            }
            else
            {
                v9 = ComParket.ComProc_Byte2Float(ret.Data,ComParketHead.HEAD_SIZE);
                v10 = ComParket.ComProc_Byte2Float(ret.Data,ComParketHead.HEAD_SIZE+4);
                tbV9.Text = v9.ToString("f4");
                tbV10.Text = v10.ToString("f4");
            }
            Win32API.MessageBeep(APIConstant.MB_ICONEXCLAMATION);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            c1 = (float)nC1.Value;
            c2 = (float)nC2.Value;
            c3 = (float)nC3.Value;
            c4 = (float)nC4.Value;
            try
            {
                k1 = (float)((c2 - c1) / (Math.Log10(v1 / v5) - Math.Log10(v1 / v3)));
                k2 = (float)((c4 - c3) / (Math.Log10(v1 / v9) - Math.Log10(v1 / v7)));
                k3 = (float)((c4 - c3) / (Math.Log10(v2 / v10) - Math.Log10(v2 / v8)));
                b1 = (float)(c2 - k1 * Math.Log10(v1 / v5));
                b2 = (float)(c4 - k2 * Math.Log10(v1 / v9));
                b3 = (float)(c4 - k3 * Math.Log10(v2 / v10));
                tbK1.Text = k1.ToString("f4");
                tbK2.Text = k2.ToString("f4");
                tbK3.Text = k3.ToString("f4");
                tbB1.Text = b1.ToString("f4");
                tbB2.Text = b2.ToString("f4");
                tbB3.Text = b3.ToString("f4");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            Win32API.MessageBeep(APIConstant.MB_ICONEXCLAMATION);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[24];
            byte[] dd;
            dd = ComParket.ComProc_Float2Byte(k1);
            for (int i = 0; i < 4; i++)
                data[i] = dd[i];
            dd = ComParket.ComProc_Float2Byte(b1);
            for (int i = 0; i < 4; i++)
                data[i+4] = dd[i];
            dd = ComParket.ComProc_Float2Byte(k2);
            for (int i = 0; i < 4; i++)
                data[i+8] = dd[i];
            dd = ComParket.ComProc_Float2Byte(b2);
            for (int i = 0; i < 4; i++)
                data[i+12] = dd[i];
            dd = ComParket.ComProc_Float2Byte(k3);
            for (int i = 0; i < 4; i++)
                data[i+16] = dd[i];
            dd = ComParket.ComProc_Float2Byte(b3);
            for (int i = 0; i < 4; i++)
                data[i+20] = dd[i];
            ComParket ret = ComParket.ComProc(ref serialPort1, 0x0009, data);
            if (ret == null)
            {
                MessageBox.Show("无法从设备中采集到数据，请检查连接情况");
            }
            else
            {
                k1 = ComParket.ComProc_Byte2Float(ret.Data, ComParketHead.HEAD_SIZE);
                b1 = ComParket.ComProc_Byte2Float(ret.Data, ComParketHead.HEAD_SIZE + 4);
                k2 = ComParket.ComProc_Byte2Float(ret.Data, ComParketHead.HEAD_SIZE + 8);
                b2 = ComParket.ComProc_Byte2Float(ret.Data, ComParketHead.HEAD_SIZE + 12);
                k3 = ComParket.ComProc_Byte2Float(ret.Data, ComParketHead.HEAD_SIZE + 16);
                b3 = ComParket.ComProc_Byte2Float(ret.Data, ComParketHead.HEAD_SIZE + 20);
                tbK1.Text = k1.ToString("f4");
                tbK2.Text = k2.ToString("f4");
                tbK3.Text = k3.ToString("f4");
                tbB1.Text = b1.ToString("f4");
                tbB2.Text = b2.ToString("f4");
                tbB3.Text = b3.ToString("f4");
                MessageBox.Show("校准成功");
            }
            Win32API.MessageBeep(APIConstant.MB_ICONEXCLAMATION);
        }
    }
}
